
#include "ReturnEnemyFlag.h"
#include "CommonBehaviours.h"
#include "../Ship.h"
#include "../GameState.h"
#include "../Team.h"

#include <cassert>
#include <iostream>

ReturnEnemyFlag::ReturnEnemyFlag(Ship *my_ship) :
    my_ship(my_ship) {
    assert(my_ship != NULL);

    my_team_id = my_ship->getControllingPlayer()->getTeamId();
    my_flag = GameState::instance().getTeam(my_team_id).getFlag();

    path_find = NULL;
}

ReturnEnemyFlag::~ReturnEnemyFlag(){
    delete path_find;
}

bool ReturnEnemyFlag::shouldIPerform(void){
    // Check to see if I have the flag.
    if(!my_ship->haveFlag()){ 
        clear();
        return false; 
    }
    
    // Otherwise its all good to go to return the flag
    return true;
}

bool ReturnEnemyFlag::perform(void){
    if(path_find == NULL){
        path_find = new PathFindBehaviour(my_ship, my_flag->getHomePosition());
    }

    if(!path_find->make_valid()){
        clear();
        path_find = new PathFindBehaviour(my_ship, my_flag->getHomePosition());
    }

    if(path_find->goToTarget()){
        clear(); 
        return true;
    }
    return false;
}

void ReturnEnemyFlag::clear(void){
    if(path_find != NULL){
        delete path_find;
        path_find = NULL;
    }
}

